<template>
  <div class="page-head">
    <div class="head-sec">
      <div class="head-base">
        <div class="base-inner">
          <div class="base-box w-1400 flex-between">
            <div class="base-left flex">
              <div class="web-title">Hi，欢迎来到药简单采购平台！</div>
              <!-- <router-link class="login" to="/pagelogin">请登录</router-link>
              <router-link class="register" to="/register"
                >免费注册</router-link
              > -->
            </div>
            <!-- <div class="base-left flex" v-if="vuex_is_login">
              <div class="web-title">欢迎访问氿洲工品</div>
              <span>
                您好,
                <b class="user-index" @click="$router.push('/userIndex')">{{
                  vuex_user.name
                }}</b>
                欢迎进入中科星选
              </span>
              <span class="logout" @click="logout()">退出登录</span>
            </div> -->

            <div class="base-right flex">
              <!-- <div class="login-action" v-if="!vuex_is_login">
                <router-link class="login" to="/pagelogin">登录</router-link>
              </div>
            <div class="login-action" v-if="!vuex_is_login">
              <router-link class="register" to="/register">注册</router-link>
            </div> -->
              <!-- <span class="u-line"></span> -->
              <div class="u-act" v-if="!vuex_is_login">
                <div>登录</div>
              </div>
              <div class="u-act flex" v-else style="cursor:pointer">
                <img class="personClass" src="@/assets/newImg/home/person.png" alt="" />
                <div>{{ vuex_user.nickname }}</div>
                <span class="u-line"></span>
                <div @click="logout()">退出登录</div>
              </div>

              <div class="u-act" v-if="vuex_is_login" style="cursor:pointer">
                <div @click="jumpCenter">会员中心</div>
                <!-- <i class="el-icon-caret-bottom"></i> -->
              </div>

              <div class="u-act">
                <!-- <router-link to="/help">联系我们</router-link> -->
                <div class="flex">
                  <img class="telClass" src="@/assets/newImg/home/tel.png" alt="" />
                  <div class="telNum">400-1234-5678</div>
                </div>
                <!-- <i class="el-icon-caret-bottom"></i> -->
              </div>
            </div>
          </div>
        </div>
      </div>

      <div class="head-search">
        <div class="search-inner">
          <div class="search-box w-1400">
            <div class="left-logo">
              <img src="@/assets/newImg/home/logo.png" @click="$router.push('/')" />
            </div>

            <div class="center-search">
              <div class="input-box">
                <input type="text" v-model="keyword" @keyup.enter="do_search()" placeholder="请输入要搜索的商品" />
                <button class="btn btn-ripple" @click="do_search()">
                  <!-- <i class="el-icon-search"></i> -->
                  <img src="@img/head/search.png" alt="">
                  搜索
                </button>
              </div>

              <!-- <div class="reci-wrap">
                <div class="reci-label">热搜词:</div>
                <div class="reci-list">
                  <div
                    class="reci"
                    v-for="(item, index) in keyword_list"
                    :key="index"
                    @click="do_search_reci(item)"
                  >
                    {{ item.title }}
                  </div>
                </div>
              </div> -->
            </div>

            <el-popover placement="right" width="400" trigger="hover">
              <div>
                <div class="item-list" v-for="item in vuex_cart_product" :key="item.id">
                  <img :src="item.goods.goodsCover" class="item-list-img" alt="" />
                  <div class="item-list-text">{{ item.goods.goodsName }}</div>
                  <div class="item-list-num">x{{ item.goodsNums }}</div>
                  <img class="item-list-laji" @click="deleteCart(item)" src="@/assets/newImg/home/laji.png" alt="" />
                </div>
                <div class="btn-car-div">
                  <div class="btn-cart" @click="$router.push('/cart')">
                    <img src="@/assets/newImg/home/cart.png" class="btn-cart-img" alt="" />
                    查看购物车
                  </div>
                  <div class="btn-cart mgl20" @click="to_pay">
                    马上去结算
                    <img src="@/assets/newImg/home/arrow.png" class="btn-cart-arrow" alt="" />
                  </div>
                </div>
              </div>
              <div class="btns flex-between" slot="reference">
                <router-link to="/cart" class="link bg">
                  <div class="btn-box">
                    <el-badge :value="vuex_cart_number" class="item" :hidden="!vuex_cart_number">
                      <div class="btn-box-img-div">
                        <img src="@/assets/newImg/home/cart.png" alt="" />
                      </div>
                    </el-badge>

                    <div class="text-div">购物车</div>
                  </div>
                </router-link>
              </div>
            </el-popover>

          </div>

        </div>
        <div class="head-nav">
          <div :class="{'head-nav-title-active': $route.path == '/', 'head-nav-title':  $route.path !=='/'}"  @click="$router.push('/')">首页</div>
          <div :class="{'head-nav-title-active': $route.path == '/newAllProduct', 'head-nav-title':  $route.path !=='/newAllProduct'}" @click="$router.push('/newAllProduct')">全部产品</div>
          <div  :class="{'head-nav-title-active': $route.path == '/groupPurchase', 'head-nav-title':  $route.path !=='/groupPurchase'}" @click="$router.push('/groupPurchase')">团购专区</div>
          <div class="head-nav-title">限时秒杀</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import page_nav from "@/components/page/page-nav.vue";

import { mapState } from "vuex";

export default {
  name: "HeaderIndex",
  components: {
    page_nav,
  },
  data() {
    return {
      showSiteMap: false, //个人中心 菜单
      showLanguage: false, //语言切换
      showContact: false, //联系我们
      show_shoujiban: false, //手机版

      list_lang: [
        { title: "中文", lang: "zh" },
        { title: "English", lang: "en" },
      ],

      keyword: "",
      //

      showSearch: false,
      keyword: "",

      search_suggest_list: [],
      disabledSearchQuery: false,
      searchLock: false, //锁定搜素

      user_menus: [
        { title: "我的订单", route: "/order-list" },
        { title: "我的售后", route: "/refund-list" },
        { title: "我的收藏", route: "/favorite-list" },
        { title: "我的足迹", route: "/browse-history" },
        { title: "地址管理", route: "/address-list" },
        { title: "我的发票", route: "/invoice-list" },
        { title: "个人信息", route: "/my-info" },
        { title: "修改密码", route: "/change-password" },
        { title: "退出登录", route: "" },
      ],
    };
  },

  computed: {
    ...mapState([
      //
      'vuex_cart_number',
      'vuex_cart_product'
    ]),

    keyword_list() {
      let arr = [];
      if (this.vuex_config.hotSearch) {
        try {
          arr = JSON.parse(this.vuex_config.hotSearch);
        } catch (error) { }
      }
      return arr;
    },
  },

  watch: {
    $route(to, from) {
      if (to.name == "product-search") {
        if (to.query.id) {
        }
      }
      this.searchLock = true;
      this.keyword = to.query.keyword;
    },
  },

  created() {
    this.keyword = this.$route.query.keyword || "";
    this.setView();
  },

  methods: {
    deleteCart(item){
      this.$httpApi('/api/v1/purchaser/del/goods/cart/'+item.id, {}, 'get').then(res=>{
        this.$store.dispatch("query_cart");
      })
    },
    //去结算
    to_pay() {
      let data_format = this.vuex_cart_product.map((v) => ({
        ...v
      }));

      this.$store.commit(
        "set_cache_payment_products",
        JSON.stringify(data_format)
      );

      this.$router.push({
        path: "/order-submit",
        query: {
          from: "heard",
        },
      });
    },
    //语言切换
    toggleLanguage(lang_curr) {
      // let lang_prev = localStorage.getItem("lang") || "zh";
      // let lang_curr = "";
      // if (lang_prev == "zh") {
      //   lang_curr = "en";
      // } else if (lang_prev == "en") {
      //   lang_curr = "zh";
      // }

      if (localStorage.getItem("lang") == lang_curr) {
        return;
      }

      this.$store.commit("set_lang", lang_curr);
      this.$i18n.locale = lang_curr;

      location.reload();
    },

    jump(route) {
      // debugger
      this.$router.push("/" + route);
    },

    goCart() {
      this.$router.push({ path: "/cart" });
    },
    goOrderAll() {
      this.$router.push({ path: "/orderAll" });
    },
    mouseover() {
      this.showSiteMap = true;
    },
    mouseout() {
      this.showSiteMap = false;
    },
    mouseoverLang() {
      this.showLanguage = true;
    },
    mouseoutLang() {
      this.showLanguage = false;
    },

    goModule(name) {
      if (name == "index") {
        this.$router.push({ path: "/" });
      } else if (name == "my") {
        this.$router.push({ path: "/info" });
      } else if (name == "login") {
        this.$router.push({ path: "login" });
      }
    },
    logout() {
      this.$store.commit("remove_vuex_user");
      // debugger
      if (this.$route.meta.requireAuth) {
        this.$router.push("/");
      }
    },
    // 
    // 搜索
    do_search() {
      this.$router.push({
        path: "/product-cates",
        query: {
          keyword: this.keyword,
        },
      });
    },
    //热搜
    do_search_reci(item) {
      this.keyword = item.title;
      this.$router.push({
        path: "/product-cates",
        query: {
          keyword: this.keyword,
        },
      });
    },

    //自定义 banner跳转
    jump_banner(item) {
      console.log({ ...item });
      let url = item.url;
      if (!url) {
        return;
      }
      window.open(url, "_blank");
    },
    ///

    setView() { },
    mouseoutSearch() { },

    toHome() {
      if (this.$route.name != "index") {
        this.$router.push("/");
      } else {
        document.documentElement.scrollTop = 0;
      }
    },

    checkClass(item) {
      let pagePath = this.$route.fullPath;

      let obj = {
        active: item.route == pagePath || item.title == this.$route.meta.root,
        "nav-item-static": "/product-cates" == item.path,
      };
      return obj;
    },

    toSearch() {
      // if (!this.keyword) {
      //   alertErr("请输入搜索关键字");
      //   return;
      // }
      let query = {};
      if (this.selectCate) {
        query.id = this.selectCate;
      }

      let keyword = (this.keyword || "").trim() || "";
      query.keyword = keyword;
      query.ms = new Date().getTime();
      this.$router.push({
        path: "/product-search",
        query: query,
      });
    },

    logout() {
      this.$store.commit("remove_vuex_user");
      // if (this.$route.meta.requireAuth) {
      //   this.$router.push("/pagelogin");
      // }
      this.$router.push("/pagelogin");
      alertSucc("退出成功");
    },
    jumpCenter(){
      let userinfo = localStorage.getItem("vuex_user");
      try {
        let userObj = JSON.parse(userinfo);
        if(userObj.user.userType == '01') {
          this.$router.push("/my-index");
        }else {
          this.$router.push("/business-index");
        }
      }catch{
        console.log('error')
      }
    }
  },
};
</script>

<style lang="less">
.item-list {
  display: flex;
  align-items: center;
  padding-bottom: 9px;
  padding-top: 13px;
  border-bottom: 1px solid #EBEBEB;

  .item-list-img {
    width: 60px;
    height: 60px;
  }

  .item-list-text {
    width: 182px;
    font-weight: 400;
    font-size: 14px;
    color: #2B2B2B;
    line-height: 18px;
    margin-left: 9px;
    margin-right: 20px;
  }

  .item-list-num {
    font-weight: 400;
    font-size: 14px;
    color: #333333;
    line-height: 24px;
    width: 50px;
  }

  .item-list-laji {
    width: 20px;
    height: 20px;
    cursor: pointer;
  }
}

.btn-car-div{
  display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 12px;
}
.mgl20{
  margin-left: 20px;
}
.btn-cart {
  width: 134px;
  height: 36px;
  background: #EF1F1F;
  border-radius: 4px 4px 4px 4px;
  display: flex;
  align-items: center;
  justify-content: center;

  .btn-cart-img {
    width: 20px;
    height: 20px;
    margin-right: 12px;
  }

  font-weight: 400;
  font-size: 14px;
  color: #FFFFFF;
  line-height: 24px;
  cursor: pointer;
  .btn-cart-arrow{
    width: 19px;
    height: 12px;
    margin-left: 12px;
  }
}

.w-nav-popover .popper__arrow {
  display: none !important;
}

.w-nav-popover {
  padding: 0 !important;
  margin-top: 2px !important;
  // border: none !important;
}

.pop-child {
  .child-item {
    text-align: center;
    display: block;
    height: 40px;
    line-height: 40px;

    background: #fff;
    font-size: 14px;
    font-family: Microsoft YaHei-Regular, Microsoft YaHei;
    font-weight: 400;
    color: #000000;

    &:hover {
      background: #009f39;
      color: #fff;
    }
  }
}
</style>



<style scoped lang="less">
.telClass {
  width: 16px;
  height: 20px;
  margin-right: 7px;
}

.telNum {
  font-weight: bold;
  font-size: 18px;
  color: #333333;
  line-height: 36px;
}

.u-act {
  font-weight: 400;
  font-size: 14px;
  color: #333333;
  margin-right: 53px;

  span {
    font-size: 14px;
    font-weight: 400;
  }
}

.u-line {
  margin: 0 15px;
  width: 1px;
  height: 16px;
  background: #bbbbbb;
}

.personClass {
  width: 18px;
  height: 18px;
  margin-left: 8px;
}

::v-deep .el-badge__content {
  border: 0px;
}

::v-deep .el-badge__content,
.el-progress.is-exception .el-progress-bar__inner {
  background: #EF1F1F;
}

.text-div {
  width: 72px;
  height: 40px;
  background: #F4F4F4;
  border-radius: 4px 4px 4px 4px;
  border: 1px solid #ECECEC;
  font-weight: 400;
  font-size: 14px;
  color: #333333;
  line-height: 40px;
  text-align: center;
}

.head-sec {}

.head-base {
  background: #F6F6F6;

  .base-inner {
    .base-box {
      height: 36px;
      font-family: Microsoft YaHei, Microsoft YaHei;
      font-weight: 400;
      font-size: 14px;
      color: #585858;
      line-height: 0px;

      .base-left {
        float: left;
        min-width: 20%;
        text-align: left;

        .web-title {
          margin-right: 40px;
        }

        a {
          font-family: Microsoft YaHei, Microsoft YaHei;
          font-weight: 400;
          font-size: 14px;
          color: #ffffff;

          &:hover {
            color: #009f39;
          }
        }

        .login {
          margin-right: 20px;
        }

        .register {}

        .logout {
          cursor: pointer;
        }
      }

      .base-right {
        a {
          color: #222222;
        }

        .login-action {
          .login {
            color: #222222;
          }

          .logout {
            color: #222222;
          }
        }

        .icon {
          width: 16px;
          margin-right: 6px;
        }


        .u-my {
          height: 36px;
        }

        .u-line {
          margin: 0 15px;
          width: 1px;
          height: 16px;
          background: #bbbbbb;
        }
      }
    }
  }
}

.head-search {

  // background: #0c0a0a;
  .search-inner {
    // height: 140px;
    display: flex;
  }

  .search-box {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-top: 26px;

    .left-logo {
      cursor: pointer;
      justify-content: flex-start;

      img {
        width: 190px;
        height: 80.3px;
        cursor: pointer;
      }

      span {
        margin-left: 10px;
        font-size: 30px;
        font-family: Microsoft YaHei;
        font-weight: 400;
        line-height: 30px;
        color: #333333;
      }
    }

    .center-search {

      .input-box {
        border-radius: 4px 4px 4px 4px;
        border: 2px solid #EF1F1F;
        display: flex;
        align-items: center;

        height: 44px;
        background: #efefef;
        overflow: hidden;

        input {
          flex: 1;
          width: 649px;
          height: 100%;
          height: 44px;
          border: none;
          outline: none;
          background: #fff;
          padding-left: 20px;
          padding-right: 30px;
          // border: 2px solid #333;
          font-size: 14px;
          font-family: Microsoft YaHei;
          color: #000;
        }

        button {
          width: 104px;
          height: 40px;
          background: #EF1F1F;
          border-radius: 0px 4px 4px 0px;
          // border: 2px solid #333;
          border: none;
          outline: none;
          cursor: pointer;

          font-family: PingFang SC, PingFang SC;
          font-weight: 500;
          font-size: 16px;
          color: #ffffff;

          i {
            color: #fff;
            font-size: 22px;
          }

          &:hover {
            filter: opacity(0.8);
          }

          img {
            margin-right: 6px;
            width: 18px;
            height: 18px;
          }
        }
      }

      .reci-wrap {
        margin-top: 10px;
        height: 20px;
        display: flex;
        align-items: center;
        font-size: 12px;

        .reci-label {
          color: #000;
          display: inline-block;
          min-width: 40px;
        }

        .reci-list {
          display: flex;
          color: #9f9f9f;

          .reci {
            margin-right: 24px;
            cursor: pointer;
            font-family: Microsoft YaHei, Microsoft YaHei;
            font-weight: 400;
            font-size: 14px;
            color: #c4c4c4;

            &:hover {
              color: #009f39;
            }
          }
        }
      }
    }

    .btns {
      .link {
        .btn-box {
          border-radius: 20px;
          width: 112px;
          height: 40px;
          position: relative;
          display: flex;
          justify-content: center;
          align-items: center;

          .btn-box-img-div {
            width: 40px;
            height: 40px;
            background: #EF1F1F;
            border-radius: 4px 4px 4px 4px;
            border: 2px solid #EF1F1F;
            display: flex;
            justify-content: center;
            align-items: center;
          }
        }

        &:hover {
          opacity: 0.75;
        }

        img {
          width: 22px;
          margin-right: 6px;
        }

        .text {
          font-family: MicrosoftYaHei, MicrosoftYaHei;
          font-weight: normal;
          font-size: 16px;
          color: #ffffff;
        }

        .cart-num {
          position: absolute;
          right: -10px;
          top: -5px;
          min-width: 23px;
          height: 18px;
          background: #ffaa2a;
          border-radius: 9px 9px 9px 9px;

          font-family: MicrosoftYaHei, MicrosoftYaHei;
          font-weight: normal;
          font-size: 12px;
          color: #FFFFFF;
        }
      }
    }
  }
}

//
//
//
//
//

.head-nav {
  background: #fff;
  margin-left: 614px;
  display: flex;
}

.head-nav-title {
  font-weight: 500;
  font-size: 18px;
  color: #1A1A1A;
  line-height: 40px;
  margin-right: 40px;
  cursor: pointer;
}

.head-nav-title-active {
  font-weight: 500;
  font-size: 18px;
  color: #EF1F1F;
  line-height: 40px;
  border-bottom: 3px solid #EF1F1F;
  margin-right: 40px;
  cursor: pointer;
}

.page-head {
  //position: sticky;
  z-index: 100;
  top: 0;
  left: 0;
  right: 0;
  line-height: 0;
  padding: 0;
  // border-bottom: 4px solid #009a44;
  // box-shadow: 0px 3px 10px 1px rgba(0, 0, 0, 0.16);
  background: #fff;
}

.header-inner {
  position: relative;
  width: 1400px;
  height: 150px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .logo-wrap {
    display: flex;
    align-items: center;

    .logo-box {
      cursor: pointer;

      img {
        width: 141px;
        height: 82px;
      }
    }
  }

  .nav-wrap {
    flex: 1;
    margin-left: 60px;
    margin-right: 92px;
  }

  .nav-list {
    height: 100%;

    .nav-item {
      // flex: 1;
      text-align: center;

      &.nav-item-static {
        position: static;
      }

      .nav-link {
        position: relative;
        display: inline-block;
        height: 50px;
        line-height: 50px;
        font-family: Poppins, Poppins;
        font-weight: 600;
        font-size: 17px;
        color: #333333;

        &::after {
          content: "";
          position: absolute;
          bottom: 0;
          left: 50%;
          transform: translate(-50%);
          width: 0;
          height: 2px;
          background: #009f39;
          transition: 0.3s;
        }
      }

      &:hover {
        .nav-link::after {
          width: 36px;
        }
      }

      &.active {
        .nav-link {
          // font-weight: bold;
          color: #009f39;

          &::after {
            // width: 36px;
          }
        }
      }

      a {
        color: #222;
        width: 100%;
        font-size: 16px;
        font-family: Microsoft YaHei-Regular, Microsoft YaHei;
        font-weight: 400;
        color: #333;
      }
    }
  }
}

.user-act {
  width: 440px;
  width: fit-content;
}

.center-search-wrap {
  position: relative;
  margin-right: 25px;

  &:hover {
    .search-suggest-list {
      display: block;
    }
  }

  .center-search {
    width: 270px;
    border-bottom: 1px solid #999999;

    display: flex;
    align-items: center;
    align-items: stretch;

    border-radius: 5px;
    position: relative;
    overflow: hidden;

    .search-box {
      // border: 1px solid #aaa;
      display: flex;
      align-items: center;
      overflow: hidden;
      // height: 100%;

      input {
        display: block;
        width: 317px;
        height: 38px;
        padding-left: 10px;

        &::-webkit-input-placeholder {
          font-weight: normal;
          font-size: 14px;
          color: #909090;
        }
      }

      .btn-search {
        display: flex;
        justify-content: center;
        align-items: center;
        position: absolute;
        right: 0;
        width: 40px;
        height: 40px;
        background: #009f39;
        border-radius: 0px 6px 6px 0px;
        font-weight: normal;
        font-size: 16px;
        color: #ffffff;
        background: transparent;

        &:hover {
          opacity: 1 !important;
        }

        .img-search {
          width: 20px;
          height: 20px;
          cursor: pointer;
          transition: 0.3s;
          margin-right: 11px;

          &:hover {
            opacity: 0.85;
          }
        }
      }
    }
  }

  .search-suggest-list {
    display: none;
    position: absolute;
    z-index: 100;
    left: 0;
    right: 0;
    top: 43px;
    border: 1px solid #ddd;
    border-top: none;
    background: #fff;
    border-radius: 6px;
    overflow: hidden;

    .search-suggest-item {
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
      line-height: 3.5rem;
      height: 3.5rem;
      padding: 0 5px;
      text-align: left;
      transition: 0.3s;

      &:hover {
        background: #f5f5f5;
      }

      a {
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
        display: block;
        width: 100%;
        height: 100%;
        color: #888;
        font-size: 1.3rem;

        &:hover {
          color: #009f39;
        }
      }
    }
  }
}

.info-right {
  display: flex;
  align-items: center;

  .log-box {
    display: flex;
    align-items: center;
  }

  a {
    color: #fff;
    display: flex;
    align-items: center;

    img {
      width: 30px;
      height: 30px;
    }
  }

  .user-login {
    width: 30px;
    height: 30px;
  }

  .user-reg {
    margin: 0 16px;
    width: 30px;
    height: 30px;
  }

  .cart-image {
    width: 30px;
    height: 30px;
  }

  .shopcart {
    position: relative;
    width: 30px;
    height: 30px;
    display: flex;
    justify-content: center;
    align-items: center;

    img {
      width: 30px;
      height: 30px;
    }

    .cart-num {
      display: flex;
      justify-content: center;
      align-items: center;
      position: absolute;
      right: -10px;
      top: -10px;
      min-width: 18px;
      min-height: 18px;
      padding: 0 3px;
      background: #ff3b30;
      border: 1px solid #ffffff;
      border-radius: 50%;
      font-size: 12px;
    }
  }
}

.user-login-info {
  position: absolute;
  right: 0;
  top: 12px;

  .avatar-box {
    width: 35px;
    height: 35px;
    border-radius: 50%;
    overflow: hidden;
    cursor: pointer;

    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }

  .phone {
    margin: 0 16px;
    font-family: Arial, Arial;
    font-weight: 400;
    font-size: 16px;
    color: #999999;

    &:hover {
      color: #009f39;
    }
  }

  .logout {
    font-family: Arial, Arial;
    font-weight: 400;
    font-size: 16px;
    color: #999999;

    &:hover {
      color: #009f39;
    }
  }
}

@media screen and (max-width: 1600px) {
  .header-inner .nav-list .nav-item {
    margin-left: 10px;
    min-width: 80px;
  }

  .left-select {
    width: 160px !important;
  }
}

@media screen and (max-width: 1520px) {
  .header-inner .nav-list .nav-item {
    margin-left: 10px;
    min-width: 80px;
  }

  .left-select {
    width: 150px !important;
  }
}
</style>

<style scoped lang="less" src="@/assets/h5css/zujian/pageHeader.less"></style>
